import request from "@/utils/request";

/**
 * 积分提现相关接口
 */
const api_name = '/api/withdraw';

// 定义提现查询参数类型 (WithdrawalParams)
export interface WithdrawalParams {
  /**
   * 结束时间
   */
  endTime?: string;
  /**
   * 排序方式
   */
  isAsc?: boolean;
  /**
   * 关键字
   */
  keyword?: string;
  /**
   * 排序字段
   */
  orderBy?: string;
  /**
   * 当前页码
   */
  pageNum: number;
  /**
   * 每页数量
   */
  pageSize: number;
  /**
   * 开始时间
   */
  startTime?: string;
  /**
   * 提现状态
   */
  status?: Status;
}

/**
 * 提现状态
 */
export enum Status {
  Fail = "FAIL",
  Processing = "PROCESSING",
  Success = "SUCCESS",
  Pending = "PENDING"
}

/**
 * 获取提现申请记录列表
 * @param data 查询参数
 */
export function getWithdrawalList(data: WithdrawalParams) {
  return request('POST', `${api_name}/list`, data);
}

/**
 * 删除提现申请记录
 * @param id 提现申请记录ID
 */
export function deleteWithdrawal(id: number): Promise<any> {
  if (!Number.isInteger(id) || id <= 0) {
    throw new Error("Invalid withdrawal ID");
  }
  return request('DELETE', `${api_name}/delete/${id}`);
}

/**
 * 更新提现申请记录状态
 * @param param 提现状态更新参数
 */
export interface WithdrawalStatusParams {
  /**
   * 提现申请记录ID
   */
  id: number;
  /**
   * 提现申请记录状态
   */
  status: Status;
}
// 确保使用application/json content-type
// 或者使用查询参数方式
export function updateWithdrawalStatus(param: WithdrawalStatusParams) {
  if (!Number.isInteger(param.id) || param.id <= 0) {
    throw new Error("Invalid withdrawal ID");
  }
  return request('PUT', `${api_name}/update?id=${param.id}&status=${param.status}`);
}
/**
 * 获取提现统计记录列表
 * @param params 查询参数
 */
export function getWithdrawalStats(params: WithdrawalParams) {
  return request('POST', `${api_name}/stats`, params);
}

/**
 * 获取提现申请记录详情
 * @param param 查询参数
 */
export interface WithdrawalDetailParams {
  /**
   * 提现申请记录ID
   */
  id: number;
  /**
   * 用户ID
   */
  userId: number;
}
export function getWithdrawalDetail(param: WithdrawalDetailParams) {
  return request('GET', `${api_name}/detail`, { params: param });
}

/**
 * 获取用户总积分
 * @param id 用户ID
 */
export function getUserTotalPoints(id: number) {
  return request('GET', `${api_name}/total?userId=${id}`);
}

/**
 * 获取用户提现记录列表
 * @param params 查询参数
 * @return 用户提现记录列表
 */
export interface UserWithdrawalListParams {
  /**
   * 页码
   */
  pageNum: number;
  /**
   * 页大小
   */
  pageSize: number;
  /**
   * 用户ID
   */
  userId: number;
}
export function getUserWithdrawalList(params: UserWithdrawalListParams) {
  return request('GET', `${api_name}/record`, { params }); 
}

/**
 * 获取用户提现信息
 * @param id 用户ID
 * @return 用户提现信息
 */
export function getUserWithdrawalUserInfo(id: number) {
  return request('GET', `${api_name}/userinfo/${id}`);
}